package com.oracle.coherence.patterns.security.extend;

import com.oracle.coherence.patterns.security.accesscontrol.BasePermission;

import java.io.Serializable;

/**
 * @author Jonathan Knight
 */
public class InvocationPermission extends BasePermission implements Serializable {

    /**
     * Create a new permission for the specified target cache(s) and for the
     * specified action(s).
     * <p/>
     * The target is either an asterix to represent all caches or a comma delimited
     * list of cache names. Cache names may be suffixed with an asterix to represent
     * wild-carded cache names.<b/>
     * For example<b/>
     * The target <code>test-*</code> matches all cache names that start with <code>test-</code>
     * <p/>
     * The action can be a single action or a comma-delimited list of actions. The action names
     * must match valid names of actions in the {@link CacheAction} class.
     * <p/>
     *
     * @param target - the target cache this permission is for
     * @param action - the action this permission represents
     */
    public InvocationPermission(String target, String action) {
        super(target, action);
    }

    /**
     * Parse the specified comma-delimited list of actions and
     * set the actionMask field with the bit-mask representing
     * the listed actions.
     * <p/>
     *
     * @param actionList - the comman-delimited list of actions.
     * @throws IllegalArgumentException - if the actionList parameter is null
     *                                  or an empty string
     */
    protected int parseAction(String actionList) {
        return parseAction(InvokeAction.ALL, actionList);
    }

    /**
     * Returns a String representation of the actions represented by the
     * specified action mask. The action mask is a bit-mapped integer
     * where each bit represents a different action.
     * <p/>
     *
     * @param action - the action mask to convert to a string
     * @return a string representation of the specified action mask
     */
    public String formatAction(int action) {
        return formatAction(InvokeAction.ALL, InvokeAction.NONE, action);
    }

}